package org.duang.plugins;

import java.util.Iterator;
import java.util.List;

import org.duang.config.InstanceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Plugins {
	
	private static Logger logger = LoggerFactory.getLogger(Plugins.class);
	
	public static void init() {
		start();
	}
	
	public static void start() {		
		for(Iterator<IPlugin> it = InstanceFactory.getPluginList().iterator(); it.hasNext();){
			IPlugin plugin = it.next();
			if(null != plugin){
				try{
					plugin.start();
					logger.warn(plugin.getClass().getName() +" start success...");
				}catch(Exception ex){
					logger.warn(plugin.getClass().getName() +" start fail: " + ex.getMessage());
				}
			}
		}
	}
	
	public static void stop(){
		List<IPlugin> plugins = InstanceFactory.getPluginList();
		if(null != plugins && !plugins.isEmpty()){
			for(int i=plugins.size()-1; i>=0; i--){
				try {
					plugins.get(i).stop();
					logger.warn(plugins.get(i).getClass().getName() +" stop success...");
				}catch (Exception ex) {
					logger.warn(plugins.get(i).getClass().getName() + " stop fail: " + ex.getMessage() );
				}
			}
		}
	}

}
